#include"stack.h"

void STInit(ST* s)
{
    assert(s);
    s->_a = NULL;
    s->_top = s->_capacity = 0;
}
void STDestory(ST* s)
{
    assert(s);
    if(s->_a)
    {
        free(s->_a);
        s->_a = NULL;
        s->_capacity = s->_top = 0;
    }
}
void STPush(ST* s, STDataType x)
{
    assert(s);
    if(s->_top == s->_capacity)
    {
        int newcapacity = s->_capacity == 0 ? 4 : s->_capacity * 2;
        STDataType* tmp = (STDataType*)realloc(s->_a, sizeof(STDataType) * newcapacity);
        if(tmp == NULL)
        {
            perror("realloc fail");
            exit(-1);
        }
        s->_a = tmp;
        s->_capacity = newcapacity;
    }

    s->_a[s->_top] = x;
    s->_top++;
}
void STPop(ST* s)
{
    assert(s);
    assert(!STEmpty(s));
    s->_top--;
}
STDataType STTop(ST* s)
{  
    assert(s);
    assert(!STEmpty(s));
    return s->_a[s->_top - 1];
}
bool STEmpty(ST* s) {assert(s); return s->_top == 0;} 
int STSize(ST* s) {assert(s); return s->_top;}
